package com.hy.frame.util;

import androidx.annotation.Nullable;

import java.util.HashMap;
import java.util.Map;


public class DataManager implements com.hy.frame.common.IDataCache {

    /**
     * 全局数据
     */
    private Map<String, Object> maps = null;

    @Override
    public void putValue(String key, @Nullable Object value) {
        if (maps == null) {
            maps = new HashMap<>();
        }
        if (value == null)
            maps.remove(key);
        else
            maps.put(key, value);
    }

    @SuppressWarnings("unchecked")
    @Nullable
    @Override
    public <T> T getValue(String key) {
        Object obj = maps != null ? maps.get(key) : null;
        if (obj != null) {
            return (T) obj;
        }
        return null;
    }

    @Override
    public void clear() {
        if (maps != null)
            maps.clear();
        maps = null;
    }

    private static class ClassHolder {
        private static final DataManager INSTANCE = new DataManager();
    }

    public static com.hy.frame.common.IDataCache getInstance() {
        return ClassHolder.INSTANCE;
    }
}
